package org.leetcode.easy.leetcode392;

public class Solution {
    public boolean isSubsequence(String s, String t) {

        int n1 = s.length();
        int n2 = t.length();

        if (n1 > n2) {
            return false;
        }
        if (s == t) {
            return true;
        }

        int[][] dp = new int[n1 + 1][n2 + 1];

        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (s.charAt(i-1) == t.charAt(j-1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    //t指向的字符与t指向的字符不匹配，需要删除t指向字符。
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }

        return dp[n1][n2] == n1;
    }
}
